﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Configuration;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace AgileDashboard.Data
{
    public class TfsDataLoader
    {


        public Uri TfsUrl
        {
            get
            {
                return new Uri(ConfigurationManager.AppSettings["TfsUrl"]);
            }
        }

        public TfsTeamProjectCollection ConnectToTfsProgrammatically()
        {
            return TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsUrl);
        }

        public ProjectCollection GetAllTeamProjects()
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsUrl);
            var wiStore = tfs.GetService<WorkItemStore>();

            return wiStore.Projects;

        }


        public Project GetTeamProjectByName(string name)
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsUrl);
            var wiStore = tfs.GetService<WorkItemStore>();

            foreach (Project proj in wiStore.Projects)
            {
                if (proj.Name == name)
                    return proj;
            }

            return null;

        }

        public Project GetTeamProjectById(int id)
        {
            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsUrl);
            var wiStore = tfs.GetService<WorkItemStore>();

            foreach (Project proj in wiStore.Projects)
            {
                if (proj.Id == id)
                    return proj;
            }

            return null;

        }



        public TeamProject GetProjectDetailsByName(string projectName)
        {




            var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsUrl);
            var vsStore = tfs.GetService<VersionControlServer>();

            return vsStore.TryGetTeamProject(projectName);
        }






        public List<WorkItem> GetWorkItemsfromQuery(string queryText, Project project)
        {

            queryText = queryText.Replace("@project",String.Format("'{0}'",project.Name));


            List<WorkItem> workItems = new List<WorkItem>();


            Query query = new Query(project.Store, queryText);
            WorkItemCollection wc = query.RunQuery();

            foreach (WorkItem wi in wc)
            {
                workItems.Add(wi);



            }

            return workItems;
        }




        public WorkItem GetWorkItem(int id, Project proj)
        {
            string query = string.Format("Select ID from WorkItems where [ID] = {0}", id);
            WorkItemCollection workItems = proj.Store.Query(query);
            if (workItems != null)
                if (workItems.Count > 0)
                    return workItems[0];
            return null;
        }



        /// <summary>
        /// Get a set of WorkItems matching a set of IDs
        /// </summary>
        /// <param name="TeamProjectCollection">The team project collection.</param>
        /// <param name="batchReadParams">A collection of the IDs whose workitems to get.</param>
        /// <returns>A dictionary of ID/WorkItem pairs.</returns>
        public WorkItemCollection GetWorkItems(Project proj, BatchReadParameterCollection batchReadParams)
        {
            WorkItemStore wiStore = proj.Store;
            Query query = new Query(wiStore, "SELECT * FROM WorkItems", batchReadParams);
            return query.RunQuery();
        }


        /// <summary>
        /// Get a workitem field value, given the ID. 
        /// </summary>
        /// <returns>The WorkItem field value.</returns>
        public string GetWorkItemFieldValue(string fieldName, int id, Project proj)
        {
            WorkItem workItem = GetWorkItem(id, proj);
            return (string)workItem.Fields[fieldName].Value;
        }


    }
}
